package com.msos.servlet.PrepaidRechargeServlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.msos.model.respBean.RespBean;
import com.msos.service.PrepaidRechargeService.PrepaidRechargeService;
import com.msos.utils.CommonsUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 弟中弟：小邦
 */
@WebServlet(urlPatterns = "/prepaidRecharge2")
public class PrepaidRechargeServlet2 extends HttpServlet {

    PrepaidRechargeService prepaidRechargeService = new PrepaidRechargeService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String phone = req.getParameter("phone");
        String nextPhone = req.getParameter("nextPhone");
        String cardNumber = req.getParameter("cardNumber");
        String password = req.getParameter("password");
        String money = req.getParameter("money");
        double douMoney = Double.parseDouble(money);
        Integer rs = prepaidRechargeService.prepaidRecharge2(phone,nextPhone,cardNumber, password, douMoney);
        RespBean respBean = null;
        if(rs == CommonsUtils.RECHARGE_SUCCESS){
            respBean = RespBean.ok("充值成功");
        }else if(rs == CommonsUtils.USER_NOTFOUND_OR_PASSWORD_BAD){
            respBean = RespBean.ok("手机用户不存在");
        }else if(rs == CommonsUtils.inconformity){
            respBean = RespBean.ok("前后手机号有误");
        }else if(rs == CommonsUtils.TYPE_OF_ERROR){
            respBean = RespBean.ok("不是移动卡");
        }else if(rs == CommonsUtils.CARD_IS_NULL){
            respBean = RespBean.ok("充值卡不存在");
        }else if(rs == CommonsUtils.CARD_IS_USE){
            respBean = RespBean.error("充值卡已被使用");
        }else if(rs == CommonsUtils.CARD_TIMEOUT){
            respBean = RespBean.error("充值卡过期");
        }else if(rs == CommonsUtils.CARD_PASSWORD_ERROR){
            respBean = RespBean.error("充值卡号密码错误");
        }else {
            respBean = RespBean.error("未知错误");
        }
        resp.setContentType("application/json;charset=utf-8");
        ObjectMapper om = new ObjectMapper();
        String json = om.writeValueAsString(respBean);
        resp.getWriter().write(json);
    }
}
